package Management;


/*
* Address.java
*
* Created on June 4, 2009, 11:54 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/


//import com.cypresscreek.util.RLMXttUtility;
//import com.cypresscreek.util.exceptions.ExceptionNotifier;
//import com.itgp.mvc.DSet;

/**
*
* @author 
*/
public class Address {
    
    public static final String AVALARA_ORIGIN = "Origin";
    public static final String AVALARA_DESTINATION = "Dest";
    public static final String CALIFORNIA = "CA";
    private static final String NAME_SEPARATOR = "_";
    //
    private String name = ""; // NAME NAME AND ADDRESS FILE
    private String care = ""; // CARE OF LINE#2
    private String address = ""; // LOCAL ADDR.LINE# 3
    private String country = ""; // COUNTRY
    private String city = ""; // CITY & STATE
    private String zip = ""; // ZIP CODE
    private String state = ""; // STATE CODE
    private String shipDescription = ""; // Ship Description
    //Next ones are for Avalara
    private double shipCost; // Shipping cost
    private double tax; // Tax amount
    private double discount; // Discount percentage
    private String promoCode = "";// promo code for the order
    //
    /* The following are for XML UPS Validation */
    private int rank;
    private double quality;
    private String postalCodeLowEnd = "";
    private String postalCodeHighEnd = "";
    //
    
    /** Creates a new instance of Address */
    public Address() {
    }
    
    public String getCityNoComma() {
        String c = city;
        int pos = c.indexOf(",");
        if (pos!=-1) { //if the city has a comma, then take everything before it
            c = c.substring(0, pos);
        }
        return c;
    }
    
    public String getCity() {
        return city;
    }
    
    public void setCity(String city) {
        this.city = city;
    }
    
    public String getZip() {
        return zip;
    }
    
    public void setZip(String zip) {
        this.zip = zip;
    }
    
    public String getState() {
        return state;
    }
    
    public void setState(String state) {
        this.state = state;
    }
    
    public int getRank() {
        return rank;
    }
    
    public void setRank(int rank) {
        this.rank = rank;
    }
    
    public double getQuality() {
        return quality;
    }
    
    public void setQuality(double quality) {
        this.quality = quality;
    }
    
    public String getPostalCodeLowEnd() {
        return postalCodeLowEnd;
    }
    
    public void setPostalCodeLowEnd(String postalCodeLowEnd) {
        this.postalCodeLowEnd = postalCodeLowEnd;
    }
    
    public String getPostalCodeHighEnd() {
        return postalCodeHighEnd;
    }
    
    public void setPostalCodeHighEnd(String postalCodeHighEnd) {
        this.postalCodeHighEnd = postalCodeHighEnd;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getCare() {
        return care;
    }
    
    public void setCare(String care) {
        this.care = care;
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public String getCountry() {
        return country;
    }
    
    public void setCountry(String country) {
        this.country = country;
    }
    
    /**
     * Accessor to the firstName property.
     * @return Returns the firstName component.
     */
    public String getFirstName() {
        String[] n = this.getName().split(NAME_SEPARATOR);
        if (n.length>0) {
            return n[0];
        } else {
            return this.getName();
        }
    }
    
    
    /**
     * Accessor to the lastName property.
     * @return Returns the lastName component.
     */
    public String getLastName() {
        String[] n = this.getName().split(NAME_SEPARATOR);
        if (n.length>1) {
            return n[1];
        } else {
            return "";
        }
    }
    
    public String getNameFormatted() {
        return getFirstName() + " " +  getLastName();
    }
    
    public String getShipDescription() {
        return shipDescription;
    }
    
    public void setShipDescription(String shipDescription) {
        this.shipDescription = shipDescription;
    }
    
    /**
     * Accessor to the apartment property.
     * @return Returns the apartment component.
     */
    public String getApartment() {
        String[] n = this.getCity().split(NAME_SEPARATOR);
        if (n.length > 1) {
            return n[1];
        } else {
            return "";
        }
    }
    
    /**
     * Accessor to the city without apartment property.
     * @return Returns the city without apartment component.
     */
    public String getCityNoApartment() {
        String[] n = this.getCity().split(NAME_SEPARATOR);
        if (n.length > 0) {
            return n[0];
        } else {
            return this.getCity();
        }
    }
    
    /**
     * Accessor to the city without state or apartment property.
     * @return Returns the city without state or apartment component.
     */
    public String getCityOnly() {
        String[] n = this.getCity().split(",");
        if (n.length > 0) {
            return n[0];
        } else {
            return this.getCity();
        }
    }
/*    This
    public static double getShippingAndHandling(String cpy, double orderValue, String routingCode) {
        double value = 0.0;
        double standardFreight = 0.0;
        double byRouting = 0.0;
        byRouting = getValueByRouting(cpy, routingCode);
        if (byRouting >= 0) { // only process the next if there was a hit by routing
            standardFreight = getStandardFreightCharge(cpy, orderValue);
        }
        value = standardFreight + Math.max(0, byRouting); // max performed to avoid negatives
        return value;
    }

    private static double getStandardFreightCharge(String cpy, double orderValue) {
        double value = 0.0;
        String table = "OEP410";
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT * FROM ").append(table);
        sb.append(" WHERE ").append("CPY410 =").append(cpy);
        sb.append(" AND DTO410 >= ").append(orderValue);
        sb.append(" ORDER BY ").append("DTO410 ");
        sb.append(" fetch first row only");
        try {
            DSet dSet = RLMXttUtility.getQuery(sb.toString(), table);
            if (dSet.getRSet(table).size() > 0) {
                value = Double.parseDouble(dSet.getColumnData(table, "FRT410", 1));
            }
        } catch (Exception e) {
            ExceptionNotifier.notify(e);
        }
        return value;
    }
    
    private static double getValueByRouting(String cpy, String routing) {
        double value = -1.0;
        String table = "OEP415";
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT * FROM ").append(table);
        sb.append(" WHERE ").append("CPY415 =").append(cpy);
        sb.append(" AND RTE415 = '").append(routing).append("'");
        try {
            DSet dSet = RLMXttUtility.getQuery(sb.toString(), table);
            if (dSet.getRSet(table).size() > 0) {
                value = Double.parseDouble(dSet.getColumnData(table, "FRT415", 1));
            }
        } catch (Exception e) {
            ExceptionNotifier.notify(e);
        }
        return value;
    }
this */
    /**
     * @return the shipCost
     */
    public double getShipCost() {
        return shipCost;
    }

    /**
     * @param shipCost the shipCost to set
     */
    public void setShipCost(double shipCost) {
        this.shipCost = shipCost;
    }

    /**
     * @return the tax
     */
    public double getTax() {
        return tax;
    }

    /**
     * @param tax the tax to set
     */
    public void setTax(double tax) {
        this.tax = tax;
    }

    /**
     * @return the discount
     */
    public double getDiscount() {
        return discount;
    }

    /**
     * @param discount the discount to set
     */
    public void setDiscount(double discount) {
        this.discount = discount;
    }

    /**
     * @return the promoCode
     */
    public String getPromoCode() {
        return promoCode;
    }

    /**
     * @param promoCode the promoCode to set
     */
    public void setPromoCode(String promoCode) {
        this.promoCode = promoCode;
    }
    
}